package com.yida.gwj.deposit.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yida.common.core.constant.SecurityConstants;
import com.yida.common.core.web.controller.BaseController;
import com.yida.common.core.web.domain.AjaxResult;
import com.yida.common.core.web.page.TableDataInfo;
import com.yida.common.log.annotation.Log;
import com.yida.common.security.annotation.RequiresPermissions;
import com.yida.gwj.api.domain.GwjFinanceAccountDetail;
import com.yida.gwj.api.domain.GwjFinanceAccountMain;
import com.yida.gwj.api.domain.request.RemoteGwjFinanceAccountDetailRequest;
import com.yida.gwj.api.domain.request.RemoteGwjFinanceAccountMainRequest;
import com.yida.gwj.api.service.RemoteGwjConfigService;
import com.yida.gwj.api.service.RemoteGwjFinanceService;
import com.yida.gwj.common.domain.dto.GwjBusinessFlagDTO;
import com.yida.gwj.common.domain.dto.GwjBusinessStepDTO;
import com.yida.gwj.common.enums.GwjBusinessFlagType;
import com.yida.gwj.common.utils.GwjConvertUtil;
import com.yida.gwj.deposit.domain.GwjDepositBusiness;
import com.yida.gwj.deposit.domain.GwjDepositBusinessLog;
import com.yida.gwj.deposit.domain.dto.GwjBusinessLogDTO;
import com.yida.gwj.deposit.domain.dto.GwjOwnerDepositDetailDTO;
import com.yida.gwj.deposit.domain.request.GwjOwnerDepositBusinessBuildRequest;
import com.yida.gwj.deposit.domain.request.GwjOwnerDepositBusinessHouseRequest;
import com.yida.gwj.deposit.domain.request.GwjOwnerDepositChooseBuildRequest;
import com.yida.gwj.deposit.domain.request.GwjOwnerDepositChooseCommunityRequest;
import com.yida.gwj.deposit.domain.request.GwjOwnerDepositChooseHouseRequest;
import com.yida.gwj.deposit.domain.request.GwjOwnerDepositEditMoneyRequest;
import com.yida.gwj.deposit.domain.request.GwjOwnerDepositSaveSubmitRequest;
import com.yida.gwj.deposit.service.IGwjDepositBusinessLogService;
import com.yida.gwj.deposit.service.IGwjDepositBusinessService;
import com.yida.gwj.deposit.service.IGwjFundsSplitBusinessService;
import com.yida.gwj.deposit.service.IGwjOwnerDepositBusinessService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description 业务（业主缴存）Controller
 * @Author wws
 * @Date 2023-02-14
 */
@RestController
@RequestMapping("/owner-deposit")
@Api(tags = "业主缴存")
public class GwjOwnerDepositBusinessController extends BaseController {

    @Autowired
    private IGwjOwnerDepositBusinessService gwjOwnerDepositBusinessService;

    @Autowired
    private IGwjDepositBusinessService gwjDepositBusinessService;

    @Autowired
    private IGwjDepositBusinessLogService gwjDepositBusinessLogService;

    @Autowired
    private RemoteGwjFinanceService remoteGwjFinanceService;

    @Autowired
    private RemoteGwjConfigService remoteGwjConfigService;

    @Autowired
    private IGwjFundsSplitBusinessService gwjFundsSplitBusinessService;

    /**
     * 保存
     */
    @RequiresPermissions("gwj:owner:deposit:save")
    @ApiOperation(value = "保存")
    @PostMapping("/save")
    @Log(title = "业主缴存-保存（缴存业务管理模块）", systemName = "gwj")
    public AjaxResult save(@Validated @RequestBody GwjOwnerDepositSaveSubmitRequest saveSubmitRequest) {
        return AjaxResult.success(gwjOwnerDepositBusinessService.saveBusiness(saveSubmitRequest));
    }

    /**
     * 签署意见
     */
    @RequiresPermissions("gwj:owner:deposit:signOpinion")
    @ApiOperation(value = "签署意见")
    @PostMapping("/signOpinion")
    @Log(title = "业主缴存-签署意见（缴存业务管理模块）", systemName = "gwj")
    public AjaxResult signOpinion(@Validated(GwjDepositBusinessLog.Sign.class) @RequestBody GwjDepositBusinessLog businessLog) {
        gwjOwnerDepositBusinessService.signOpinion(businessLog);
        return AjaxResult.success();
    }

    /**
     * 回退
     */
    @RequiresPermissions("gwj:owner:deposit:fallback")
    @ApiOperation(value = "回退")
    @DeleteMapping("/fallback")
    @Log(title = "业主缴存-回退（缴存业务管理模块）", systemName = "gwj")
    public AjaxResult fallback(@Validated(GwjDepositBusinessLog.Fallback.class) @RequestBody GwjDepositBusinessLog businessLog) {
        gwjOwnerDepositBusinessService.fallback(businessLog);
        return AjaxResult.success();
    }

    /**
     * 业务作废
     */
    @RequiresPermissions("gwj:owner:deposit:invalid")
    @ApiOperation(value = "业务作废")
    @DeleteMapping("/invalidBusiness")
    @Log(title = "业主缴存-业务作废（缴存业务管理模块）", systemName = "gwj")
    public AjaxResult invalidBusiness(@Validated(GwjDepositBusinessLog.Cancellation.class) @RequestBody GwjDepositBusinessLog gwjDepositBusinessLog) {
        gwjOwnerDepositBusinessService.invalidBusiness(gwjDepositBusinessLog);
        return AjaxResult.success();
    }

    /**
     * 送件
     */
    @RequiresPermissions("gwj:owner:deposit:send")
    @ApiOperation(value = "送件")
    @PostMapping("/send")
    @Log(title = "业主缴存-送件（缴存业务管理模块）", systemName = "gwj")
    public AjaxResult sendBusiness(@Validated(GwjDepositBusinessLog.Send.class) @RequestBody GwjDepositBusinessLog businessLog) {
        gwjOwnerDepositBusinessService.send(businessLog);
        return AjaxResult.success();
    }

    /**
     * 生成流水
     */
    @RequiresPermissions("gwj:owner:deposit:generated")
    @ApiOperation(value = "生成流水")
    @PostMapping("/genFinanceStatement/{bizId}")
    @Log(title = "业主缴存-生成流水（缴存业务管理模块）", systemName = "gwj")
    public AjaxResult genFinanceStatement(@PathVariable("bizId") Long bizId) {
        gwjOwnerDepositBusinessService.genFinanceStatement(bizId);
        return AjaxResult.success();
    }

    /**
     * 修改业务信息
     */
    @RequiresPermissions("gwj:owner:deposit:edit")
    @ApiOperation("修改业务信息")
    @PutMapping("/update")
    @Log(title = "业主缴存-修改业务信息（缴存业务管理模块）", systemName = "gwj")
    public AjaxResult update(@Validated @RequestBody GwjOwnerDepositSaveSubmitRequest saveSubmitRequest) {
        gwjOwnerDepositBusinessService.update(saveSubmitRequest);
        return AjaxResult.success();
    }

    /**
     * 提交
     */
    @RequiresPermissions("gwj:owner:deposit:submit")
    @ApiOperation("提交")
    @PostMapping("/submit")
    @Log(title = "业主缴存-提交（缴存业务管理模块）", systemName = "gwj")
    public AjaxResult submit(@Validated @RequestBody GwjOwnerDepositSaveSubmitRequest saveSubmitRequest) {
        gwjOwnerDepositBusinessService.submitBusiness(saveSubmitRequest);
        return AjaxResult.success();
    }

    /**
     * 财务对账
     */
    @RequiresPermissions("gwj:owner:deposit:financeCheck")
    @PostMapping("/financeCheck/{bizId}")
    @ApiOperation("财务对账")
    @Log(title = "业务缴存-财务对账（缴存业务管理模块）", systemName = "gwj")
    public AjaxResult financeCheck(@PathVariable Long bizId) {
        gwjOwnerDepositBusinessService.financeCheck(bizId);
        return AjaxResult.success();
    }

    /**
     * 删页
     */
    @RequiresPermissions("gwj:owner:deposit:deleteBusinessData")
    @DeleteMapping("/deleteBusinessData/{bizId}")
    @ApiOperation("删页")
    @Log(title = "业主缴存-删页（缴存业务管理模块）", systemName = "gwj")
    public AjaxResult deleteBusinessData(@PathVariable Long bizId) {
        gwjOwnerDepositBusinessService.deleteBusinessData(bizId);
        return AjaxResult.success();
    }

    /**
     * 删除案例
     */
    @RequiresPermissions("gwj:owner:deposit:removeBusiness")
    @DeleteMapping("/removeBusiness/{bizId}")
    @ApiOperation("删除案例")
    @Log(title = "业主缴存-删除案例（缴存业务管理模块）", systemName = "gwj")
    public AjaxResult removeBusiness(@PathVariable("bizId") Long bizId) {
        gwjOwnerDepositBusinessService.removeBusiness(bizId);
        return AjaxResult.success();
    }

    /**
     * 查看业务流程
     */
    @ApiOperation("查询业务流程")
    @GetMapping("/flow/{bizId}")
    @RequiresPermissions("gwj:owner:deposit:log")
    public AjaxResult flow(@PathVariable("bizId") Long bizId) {
        return AjaxResult.success(gwjDepositBusinessService.getBusinessFlow(bizId, GwjBusinessFlagType.OWNER_DEPOSIT.getCode()));
    }

    /**
     * 查看业务日志
     */
    @ApiOperation("查看业务日志")
    @GetMapping("/log/{bizId}")
    @RequiresPermissions("gwj:owner:deposit:log")
    public AjaxResult log(@PathVariable Long bizId) {
        return AjaxResult.success(GwjConvertUtil.convertList(gwjDepositBusinessLogService.selectGwjDepositBusinessLogListByBizId(bizId), GwjBusinessLogDTO.class));
    }

    /**
     * 查询业务（业主缴存）列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询")
    @RequiresPermissions("gwj:owner:deposit:list")
    public TableDataInfo list(GwjDepositBusiness gwjDepositBusiness) {
        return getDataTable(gwjOwnerDepositBusinessService.getPage(gwjDepositBusiness));
    }

    /**
     * 查询业务（业主缴存）详情
     * 
     * @param bizId 业务主键
     * @return 详情对象
     */
    @GetMapping("/getDetail/{bizId}")
    @ApiOperation(value = "详情查询", response = GwjOwnerDepositDetailDTO.class)
    @RequiresPermissions("gwj:owner:deposit:detail")
    public AjaxResult getDetail(@PathVariable("bizId") Long bizId) {
        return AjaxResult.success(gwjOwnerDepositBusinessService.getDetail(bizId));
    }

    /**
     * 查询业务（业主缴存）总账详情
     * 
     * @param mainRequest 查询参数
     * @return 总账详情对象
     */
    @GetMapping("/getAccountDetail")
    @ApiOperation(value = "查询总账", response = GwjFinanceAccountMain.class)
    @RequiresPermissions("gwj:owner:deposit:accountMain")
    public TableDataInfo getAccountMain(@Validated RemoteGwjFinanceAccountMainRequest mainRequest) {
        return gwjDepositBusinessService.selectGwjFinanceAccountMainList(mainRequest);
    }

    /**
     * 查询业务（业主缴存）分账详情
     * 
     * @param detailRequest 分账查询参数
     * @return 分账详情对象
     */
    @GetMapping("/getAccountMain/{bizId}")
    @ApiOperation(value = "查询分账", response = GwjFinanceAccountDetail.class)
    @RequiresPermissions("gwj:owner:deposit:accountDetail")
    public TableDataInfo getAccountDetail(@Validated RemoteGwjFinanceAccountDetailRequest detailRequest) {
        return gwjDepositBusinessService.selectGwjFinanceAccountDetailList(detailRequest);
    }

    /**
     * 查询业务（基础数据管理模块）列表
     */
    @GetMapping("/info")
    @RequiresPermissions("gwj:owner:deposit:list")
    @ApiOperation(("查询业务流程"))
    public AjaxResult getBusinessInfo() {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("bizFlag", GwjConvertUtil.convert(remoteGwjConfigService.selectGwjConfBusinessFlagByBizFlagId(GwjBusinessFlagType.OWNER_DEPOSIT.getCode(), SecurityConstants.INNER).getData(), GwjBusinessFlagDTO.class));
        List<GwjBusinessStepDTO> steps = GwjConvertUtil.convertList(remoteGwjConfigService.selectGwjConfStepListByFlagId(GwjBusinessFlagType.OWNER_DEPOSIT.getCode(), SecurityConstants.INNER).getData(), GwjBusinessStepDTO.class);
        steps.forEach(stepDTO -> stepDTO.setConfigs(remoteGwjConfigService.selectGwjConfStepConfigInfoCodeListByBizFlagId(stepDTO.getStepId(), SecurityConstants.INNER).getData()));
        ajax.put("steps", steps);
        return ajax;
    }

    /**
     * 删除楼栋
     */
    @RequiresPermissions("gwj:owner:deposit:edit")
    @DeleteMapping("/deleteBuildData/{bizId}/{bizBuildIds}")
    @ApiOperation("删除楼栋")
    @Log(title = "业主缴存-删除楼栋（缴存业务管理模块）", systemName = "gwj")
    public AjaxResult deleteBuild(@PathVariable("bizId") Long bizId, @PathVariable("bizBuildIds") Long[] bizBuildIds) {
        gwjOwnerDepositBusinessService.deleteBuildData(bizId, bizBuildIds);
        return AjaxResult.success();
    }

    /**
     * 主页提交
     */
    @RequiresPermissions("gwj:owner:deposit:indexSubmit")
    @PostMapping("/indexSubmit")
    @ApiOperation(value = "主页提交")
    @Log(title = "业主缴存-主页提交（缴存业务管理模块）", systemName = "gwj")
    public AjaxResult indexSubmit(@RequestBody @Validated GwjDepositBusinessLog businessLog) {
        gwjOwnerDepositBusinessService.indexSubmit(businessLog);
        return AjaxResult.success();
    }

    /**
     * 选择小区
     */
    @RequiresPermissions("gwj:owner:deposit:edit")
    @PutMapping("/chooseCommunity")
    @ApiOperation("选择小区")
    @Log(title = "业主缴存-修改业务小区信息（缴存业务管理模块）", systemName = "gwj")
    public AjaxResult chooseCommunity(@Validated @RequestBody GwjOwnerDepositChooseCommunityRequest communityRequest) {
        gwjOwnerDepositBusinessService.chooseCommunity(communityRequest);
        return AjaxResult.success();
    }

    /**
     * 修改业务楼栋信息
     */
    @RequiresPermissions("gwj:owner:deposit:edit")
    @PutMapping("/editBuild")
    @ApiOperation("修改业务小区信息")
    @Log(title = "业主缴存-修改业务楼栋信息（缴存业务管理模块）", systemName = "gwj")
    public AjaxResult editBuild(@Validated @RequestBody GwjOwnerDepositBusinessBuildRequest buildRequest) {
        gwjOwnerDepositBusinessService.editBuild(buildRequest);
        return AjaxResult.success();
    }

    /**
     * 修改业务房屋信息
     */
    @RequiresPermissions("gwj:owner:deposit:edit")
    @PutMapping("/editHouse")
    @ApiOperation("修改业务房屋信息")
    @Log(title = "业主缴存-修改业务房屋信息（缴存业务管理模块）", systemName = "gwj")
    public AjaxResult editHouse(@Validated @RequestBody GwjOwnerDepositBusinessHouseRequest editHouseRequest) {
        gwjOwnerDepositBusinessService.editHouse(editHouseRequest);
        return AjaxResult.success();
    }

    /**
     * 选择楼栋
     */
    @RequiresPermissions("gwj:owner:deposit:edit")
    @PutMapping("/chooseBuilds")
    @ApiOperation("选择楼栋")
    @Log(title = "业主缴存-选择楼栋（缴存业务管理模块）", systemName = "gwj")
    public AjaxResult chooseBuilds(@Validated @RequestBody GwjOwnerDepositChooseBuildRequest chooseBuildRequest) {
        gwjOwnerDepositBusinessService.chooseBuilds(chooseBuildRequest);
        return AjaxResult.success();
    }

    /**
     * 选择房屋
     */
    @RequiresPermissions("gwj:owner:deposit:edit")
    @PutMapping("/chooseHouses")
    @ApiOperation("选择房屋")
    @Log(title = "业主缴存-选择房屋（缴存业务管理模块）", systemName = "gwj")
    public AjaxResult chooseHouses(@Validated @RequestBody GwjOwnerDepositChooseHouseRequest chooseHouseRequest) {
        gwjOwnerDepositBusinessService.chooseHouses(chooseHouseRequest);
        return AjaxResult.success();
    }

    /**
     * 删除房屋
     */
    @RequiresPermissions("gwj:owner:deposit:edit")
    @GetMapping("/deleteHousesData/{bizId}/{bizHouseIds}")
    @ApiOperation("删除房屋")
    @Log(title = "业主缴存-删除房屋（缴存业务管理模块）", systemName = "gwj")
    public AjaxResult deleteHousesData(@PathVariable("bizId") Long bizId, @PathVariable("bizHouseIds") Long[] bizHouseIds) {
        gwjOwnerDepositBusinessService.deleteHousesData(bizId, bizHouseIds);
        return AjaxResult.success();
    }

    /**
     * 修改业务金额信息
     */
    @RequiresPermissions("gwj:owner:deposit:edit")
    @PutMapping("/editMoney")
    @ApiOperation("修改业务金额信息")
    @Log(title = "业主缴存-修改业务金额信息（缴存业务管理模块）", systemName = "gwj")
    public AjaxResult editMoney(@Validated @RequestBody GwjOwnerDepositEditMoneyRequest moneyRequest) {
        gwjOwnerDepositBusinessService.editMoney(moneyRequest);
        return AjaxResult.success();
    }

}
